(function() {
	//
	var moduleMap = {};
	var noop = function() {};

	var thin = {
		//模块的存储结构
		define: function(name, dependencies, factory) {
			//注册一个新的模块，如果map中不存在，则进行注册
			if(!moduleMap[name]) {
				//进行注册，创建一个新module
				var module = {
					name: name,
					dependencies: dependencies, //原型
					factory: factory, //工厂
				};
				//将module的键值对应
				moduleMap[name] = module;
			}
			return moduleMap[name];
		},
		use: function(name) {
			var module = moduleMap[name];
			if(!module.entity) {
				var args = [];
				for(var i = 0; i < module.dependencies.length; i++) {
					if(moduleMap[module.dependencies[i]].entity) {
						args.push(moduleMap[module.dependencies[i]].entity);
					} else {
						args.push(this.use(module.dependencies[i]));
					}
				}
				module.entity = module.factory.apply(noop, args);
			}
			return module.entity;
		},
		require: function(pathArr, callback) {
			for(var i = 0; i < pathArr.length; i++) {
				var path = pathArr[i];
				if(!fileMap[path]) {
					var head = document.getElementsByTagName('head')[0];
					var node = document.createElement('script');
					node.type = 'text/javascript';
					node.async = 'true';
					node.src = path + '.js';
					node.onload = function() {
						fileMap[path] = true;
						head.removeChild(node);
						checkAllFiles();
					};
					head.appendChild(node);
				}
			}

			function checkAllFiles() {
				var allLoaded = true;
				for(var i = 0; i < pathArr.length; i++) {
					if(!fileMap[pathArr[i]]) {
						allLoaded = false;
						break;
					}
				}
				if(allLoaded) {
					callback();
				}
			}
		}
	};
	window.thin = thin;
})();